package com.gy.hsim.bservice.auth;

import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.spring.SpringUtil;

public class SSOAuth {
	
	private static final Logger log = LoggerFactory
			.getLogger(SSOAuth.class);

	
	/**
	 * 鉴权方法
	 * 
	 * @param key 
	 * @return true or false
	 */
	public static boolean auth(String key,String mid) {
		
		boolean result = false;
		
		//生成 HttpClinet 对象并设置参数 
		HttpClient httpClient = new HttpClient();
		// 设置 Http,连接超时为5秒
		httpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(5000);
		
		SystemConfig systemConfig = (SystemConfig) SpringUtil.getBean("system");

		String ss = systemConfig.getUrl();
		
		//http接口的url
		String url = StringUtils.replace(ss, "{key}", key);
				
		url = StringUtils.replace(url, "{mid}", mid);
		
		url = StringUtils.replace(url,"{accessID}", systemConfig.getAccessID());
		
		url = StringUtils.replace(url,"{accessPwd}",  systemConfig.getAccessPwd());
			
		//生成 GetMethod 对象并设置参数 
		GetMethod getMethod = new GetMethod(url);
		//设置 get 请求超时为 5 秒
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
		// 设置请求重试处理，用的是默认的重试处理：请求三次
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());

		try {
			int statusCode = httpClient.executeMethod(getMethod);
			//判断访问的状态码 
			if (statusCode != HttpStatus.SC_OK) {   
				log.info("请求出错: " + getMethod.getStatusLine());
				return false;
			}
			byte[] responseBody = getMethod.getResponseBody();
			//处理返回的内容
			String responseMsg = new String(responseBody);
			JSONObject object = JSON.parseObject(responseMsg);
			
			String retCode = object.getString("retCode");
			if("200".equals(retCode)){
				return true;
			} else {
				result = false;
			}
			//resp.setRetCode((String)object.get("retCode"));
			//resp.setMessage((String)object.get("message"));
			log.info(responseMsg);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 6.释放连接
			getMethod.releaseConnection();
		}

		return result;
	}
	
	/**
	 * 互动消费者鉴权方法
	 * 
	 * @param key 
	 * @return true or false
	 */
	public static boolean consumerAuth(String custId,String channelType,String loginToken) {
		
		boolean result = false;
		
		//生成 HttpClinet 对象并设置参数 
		HttpClient httpClient = new HttpClient();
		// 设置 Http,连接超时为5秒
		httpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(5000);
		
		SystemConfig systemConfig = (SystemConfig) SpringUtil.getBean("system");

		String ss = systemConfig.getUrl();
		//http接口的url
		String url = StringUtils.replace(ss, "{custId}", custId);
				
		url = StringUtils.replace(url, "{channelType}", channelType);
		
		url = StringUtils.replace(url,"{loginToken}", loginToken);
				
		log.info("【调用鉴权的URL:】" + url);
			
		//生成 GetMethod 对象并设置参数 
		GetMethod getMethod = new GetMethod(url);
		//设置 get 请求超时为 5 秒
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
		// 设置请求重试处理，用的是默认的重试处理：请求三次
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());

		try {
			int statusCode = httpClient.executeMethod(getMethod);
			//判断访问的状态码 
			if (statusCode != HttpStatus.SC_OK) {   
				log.info("请求出错: " + getMethod.getStatusLine());
				return false;
			}
			byte[] responseBody = getMethod.getResponseBody();
			//处理返回的内容
			String responseMsg = new String(responseBody);
			JSONObject object = JSON.parseObject(responseMsg);
			
			String retCode = object.getString("retCode");
			if("200".equals(retCode)){
				return true;
			} else {
				result = false;
			}
			//resp.setRetCode((String)object.get("retCode"));
			//resp.setMessage((String)object.get("message"));
			log.info(responseMsg);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 6.释放连接
			getMethod.releaseConnection();
		}

		return result;
	}
	
	/**
	 * 互动操作员鉴权方法
	 * 
	 * @param key 
	 * @return true or false
	 */
	public static boolean opratorAuth(String custId,String entResNo,String channelType,String loginToken) {
		
		boolean result = false;
		
		//生成 HttpClinet 对象并设置参数 
		HttpClient httpClient = new HttpClient();
		// 设置 Http,连接超时为5秒
		httpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(5000);
		
		SystemConfig systemConfig = (SystemConfig) SpringUtil.getBean("system");

		String ss = systemConfig.getOperatorAuthenticUrl();
		//http接口的url
		String url = StringUtils.replace(ss, "{custId}", custId);
				
		url = StringUtils.replace(url, "{channelType}", channelType);
		
		url = StringUtils.replace(url,"{loginToken}", loginToken);
		
		url = StringUtils.replace(url,"{entResNo}", entResNo);
				
		log.info("【调用鉴权的URL:】" + url);
			
		//生成 GetMethod 对象并设置参数 
		GetMethod getMethod = new GetMethod(url);
		//设置 get 请求超时为 5 秒
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
		// 设置请求重试处理，用的是默认的重试处理：请求三次
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());

		try {
			int statusCode = httpClient.executeMethod(getMethod);
			//判断访问的状态码 
			if (statusCode != HttpStatus.SC_OK) {   
				log.info("请求出错: " + getMethod.getStatusLine());
				return false;
			}
			byte[] responseBody = getMethod.getResponseBody();
			//处理返回的内容
			String responseMsg = new String(responseBody);
			JSONObject object = JSON.parseObject(responseMsg);
			
			String retCode = object.getString("retCode");
			if("200".equals(retCode)){
				return true;
			} else {
				result = false;
			}
			//resp.setRetCode((String)object.get("retCode"));
			//resp.setMessage((String)object.get("message"));
			log.info(responseMsg);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 6.释放连接
			getMethod.releaseConnection();
		}

		return result;
	}

}
